function loadClsViewXML(clsNameParam) {
	var input = clsNameParam;
	if (input == null || input == '')
		input = $F('clsName');

	new Ajax.Request(
			'../lims/clsViewer',
			{
				method :'post',
				parameters : {
					clsName :input
				},
				onSuccess : function(transport) {
					var clsView = $('clsViewer');
					var response = transport.responseText;
					clsView.update(response);
				},

				onFailure : function() {
					clsView
							.update('Sorry, no cls definition found or service is not available.');
				}
			});
}

function loadTools(clsNameParam, oidParam) {
	var input = clsNameParam;
	if (input == null || input == '')
		input = $F('clsName');

	new Ajax.Request('../lims/tools.jsp', {
		method :'post',
		parameters : {
			clsName :input,
			oid :oidParam
		},
		onSuccess : function(transport) {
			var toolContent = $('tools');
			var response = transport.responseText;
			toolContent.update(response);
		},
		onFailure : function() {
			toolsDiv.update('Failed to get tools.');
		}
	});
}

function loadMetaInfo(clsName, oid) {
	new Ajax.Request('../lims/meta.jsp', {
		method :'get',
		onSuccess : function(transport) {
			var toolBox = $('meta');
			toolBox.update(transport.responseText);
		},
		onFailure : function() {
			toolBox.update('Failed to get meta information.');
		}
	});

}

function loadEditForm(clsName, oid) {
	var editUrl = '../lims/formEditInstance.jsp?clsName=' + clsName + '&oid='
			+ oid + '&action=edit';

	new Ajax.Request(editUrl, {
		method :'get',
		onSuccess : function(transport) {
			var instance = $('instance');
			var response = transport.responseText;
			instance.update(response);
		},

		onFailure : function() {
			alert('request for edit form failed');
		}
	});
}

function reloadPage() {
	window.reload();
}

function loadInstanceSelector(relationshipName, targetClsNameInput,
		srcClsNameInput, sourceOidInput) {
	new Ajax.Request(
			'../lims/instanceSelector.jsp',
			{
				method :'post',
				parameters : {
					relationship :relationshipName,
					targetCls :targetClsNameInput,
					srcCls :srcClsNameInput,
					sourceOid :sourceOidInput
				},
				onSuccess : function(transport) {
					var instanceSelectorDiv = $('instanceSelector_' + targetClsNameInput);
					var response = transport.responseText;
					instanceSelectorDiv.update(response);
				},

				onFailure : function() {
					clsView
							.update('Sorry, no cls definition found or service is not available.');
				}
			});
}

/** *** FUNCTIONS FOR IN PLACE EDITORS ************** */


function loadInPlaceEdit(elementName, clsName, attributeName, oid) {
	var tdElement = $(elementName);
	var newIDName = elementName + '_edit';
	var desc = tdElement.firstDescendant();

	if (desc != null) {
		if (desc.identify() == newIDName) {
			tdElement.onclick = null;
			return;
		}
	}
	new Ajax.Request(
			'../lims/formInPlaceEdit.jsp',
			{
				method :'request',
				parameters : {
					elementID :elementName,
					cls :clsName,
					slot :attributeName,
					oid :oid,
					oldHTML : tdElement.innerHTML
				},

				onSuccess : function(transport) {
					var response = transport.responseText;
					tdElement.update(response);
					tdElement.stopObserving('click', loadInPlaceEdit(
							elementName, clsName, attributeName, oid));
				},
				onFailure : function() {
					alert('Sorry, in place edit is either disabled or has reached an error. Try Edit instance.');
				}
			});
}

// restore elementName with old information and re-enable onclick event for the
// element
function restore(elementName, clsName, attributeName, oid, oldHTML) {
	var ele = $(elementName);
	ele.observe('click', function(event) {
		loadInPlaceEdit(elementName, clsName, attributeName, oid);
	});
	setTimeout("setElementContent('" + elementName + "', '" + oldHTML + "')",
			10);
}

function setElementContent(elementName, oldHTML) {
	$(elementName).update(oldHTML);
}

function submitIPE(attributeID, formID) {
	var containerElement = $F('elementID');
	var cls = $F('cls');
	var slot = $F('slot');
	var oid = $F('oid');
	var formHTML = $(containerElement).innerHTML;

	new Ajax.Request(
			'../lims/actionInPlaceEdit',
			{
				method :'post',
				parameters :$(formID).serialize(false),
				onSuccess : function(transport) {
					$(containerElement).update(transport.responseText);
					$(containerElement).observe('click', function(event) {
						loadInPlaceEdit(containerElement, cls, slot, oid);
					});
				},
				onFailure : function(transport) {
					$(containerElement).update(formHTML);
					$(containerElement).insert({top: '<div class=\"errorBox_small\">Saving failed due to error.	</div>'});
				}
			});
}

/** *** End functions for in place editors *************** */

